/**
 *	@file	pgradio_key.c
 *	@brief	ポケギア　キー取得
 *	@author	Miyuki Iwasawa
 *	@date	07.10.16
 */

#include "common.h"
#include "system/procsys.h"
#include "system/arc_util.h"
#include "system/arc_tool.h"
#include "system/bmp_list.h"
#include "system/bmp_menu.h"
#include "system/pm_str.h"
#include "system/msgdata.h"
#include "system/fontproc.h"
#include "system/snd_tool.h"
#include "system/clact_util.h"
#include "system/clact_tool.h"
#include "system/render_oam.h"
#include "system/wipe.h"
#include "system/buflen.h"
#include "gflib/touchpanel.h"
#include "application/app_tool.h"
#include "application/pokegear.h"
#include "msgdata/msg.naix"

#include "../pgear_sys.h"
#include "../pgear_frm.h"
#include "../pgear_act.h"
#include "pgradio_sys.h"
#include "pgradio_bg.h"
#include "pgradio_sub.h"

#define __PGRADIO_KEY_H_GLOBAL
#include "pgradio_key.h"

static int radiokey_ContTouchNormal(PGRADIO_SYS_WORK* wk,BOOL* touch_f);
static int radiokey_ContTouchDrug(PGRADIO_SYS_WORK* wk);
static u8 radiokey_CurrentChannelSet(PGRADIO_SYS_WORK* wk,s16 x,s16 y);
static void radiokey_SwitchOn(PGRADIO_SYS_WORK* wk,u8 pos);


static const RECT_HIT_TBL SelectArea_TpCircle = {
//	{0,191,0,255}, ty,by,lx,rx
	TP_USE_CIRCLE,AREA_PX,AREA_PY,AREA_R,
};

static const RECT_HIT_TBL ChJohto_Circle[] = {
	//ミュージック
	{TP_USE_CIRCLE,CHA_MUSIC_PX,CHA_MUSIC_PY,CHA_REC_R00},
	{TP_USE_CIRCLE,CHA_MUSIC_PX,CHA_MUSIC_PY,CHA_REC_R03},
	//ポケモン
	{TP_USE_CIRCLE,CHA_POKEMON_PX,CHA_POKEMON_PY,CHA_REC_R00},
	{TP_USE_CIRCLE,CHA_POKEMON_PX,CHA_POKEMON_PY,CHA_REC_R04},
	//街角
	{TP_USE_CIRCLE,CHA_TOWN_PX,CHA_TOWN_PY,CHA_REC_R00},
	{TP_USE_CIRCLE,CHA_TOWN_PX,CHA_TOWN_PY,CHA_REC_R03},
	//バラエティ
	{TP_USE_CIRCLE,CHA_MULTI_PX,CHA_MULTI_PY,CHA_REC_R00},
	{TP_USE_CIRCLE,CHA_MULTI_PX,CHA_MULTI_PY,CHA_REC_R04},
	{RECT_HIT_END,0,0,0},
};

static const RECT_HIT_TBL ChKanto_Circle[] = {
	//ミュージック
	{TP_USE_CIRCLE,CHA_MUSIC_PX,CHA_MUSIC_PY,CHA_REC_R00},
	{TP_USE_CIRCLE,CHA_MUSIC_PX,CHA_MUSIC_PY,CHA_REC_R03},
	//ポケモン
	{TP_USE_CIRCLE,CHA_POKEMON_PX,CHA_POKEMON_PY,CHA_REC_R00},
	{TP_USE_CIRCLE,CHA_POKEMON_PX,CHA_POKEMON_PY,CHA_REC_R04},
	//街角
	{TP_USE_CIRCLE,CHA_TOWN_PX,CHA_TOWN_PY,CHA_REC_R00},
	{TP_USE_CIRCLE,CHA_TOWN_PX,CHA_TOWN_PY,CHA_REC_R03},
	{RECT_HIT_END,0,0,0},
};

static const RECT_HIT_TBL ChKantoEx_Circle[] = {
	//ミュージック
	{TP_USE_CIRCLE,CHA_MUSIC_PX,CHA_MUSIC_PY,CHA_REC_R00},
	{TP_USE_CIRCLE,CHA_MUSIC_PX,CHA_MUSIC_PY,CHA_REC_R03},
	//トレーナー
	{TP_USE_CIRCLE,CHA_POKEMON_PX,CHA_POKEMON_PY,CHA_REC_R00},
	{TP_USE_CIRCLE,CHA_POKEMON_PX,CHA_POKEMON_PY,CHA_REC_R04},
	//街角
	{TP_USE_CIRCLE,CHA_TOWN_PX,CHA_TOWN_PY,CHA_REC_R00},
	{TP_USE_CIRCLE,CHA_TOWN_PX,CHA_TOWN_PY,CHA_REC_R03},
	//バラエティ
	{TP_USE_CIRCLE,CHA_MULTI_PX,CHA_MULTI_PY,CHA_REC_R00},
	{TP_USE_CIRCLE,CHA_MULTI_PX,CHA_MULTI_PY,CHA_REC_R04},
	//ポケモンの笛
	{TP_USE_CIRCLE,CHA_POKE_PX,CHA_POKE_PY,CHA_REC_R00},
	{TP_USE_CIRCLE,CHA_POKE_PX,CHA_POKE_PY,CHA_REC_R01},
	{RECT_HIT_END,0,0,0},
};

static const RECT_HIT_TBL ChKantoNone_Circle[] = {
	{RECT_HIT_END,0,0,0},
};

static const RECT_HIT_TBL ChAlf_Circle[] = {
//	{0,191,0,255}, ty,by,lx,rx
	{TP_USE_CIRCLE,CHA_ALF_PX,CHA_ALF_PY,CHA_REC_R00},
	{TP_USE_CIRCLE,CHA_ALF_PX,CHA_ALF_PY,CHA_REC_R03},
	{RECT_HIT_END,0,0,0},
};
static const RECT_HIT_TBL ChRocket_Circle[] = {
//	{0,191,0,255}, ty,by,lx,rx
	{TP_USE_CIRCLE,CHA_ROCKET_PX,CHA_ROCKET_PY,CHA_REC_R05},
	{TP_USE_CIRCLE,CHA_ROCKET_PX,CHA_ROCKET_PY,CHA_REC_R06},
	{RECT_HIT_END,0,0,0},
};

///チャンネルテーブル
static const RECT_HIT_TBL* ChAreaTbl[RADIO_AREA_MAX] = {
	ChJohto_Circle,
	ChKanto_Circle,
	ChKantoEx_Circle,
	ChKantoNone_Circle,
	ChAlf_Circle,
	ChRocket_Circle,
	ChRocket_Circle,	//<アジト怪電波の領域判定はロケット団声明と同じでいいので使いまわし
};

///ショートカットポイント
static const u8 ChTblShortcutJ[] = {
 0,1,2,3
};

/*
 *	@brief	通常アプリキー操作
 */
int PGRadioKey_ContKey(PGRADIO_SYS_WORK* wk)
{
	u8	pos;
	s16	dx = 0,dy = 0,ox,oy;

	if(sys.trg & PAD_BUTTON_B){	//システムキーモードに戻る
		wk->sys->control = PGCONT_SYS;
		CursorMvMan_SetCursorDrawFlag(wk->sys->curMan,PGKM_SYS_PARAM,TRUE);
		CursorMvMan_SetCurrent(wk->sys->curMan,PGKM_SYS_PARAM,PGFrame_GetSCurModePosNo(wk->sys));
		Snd_SePlay(SE_PGEAR_CANCEL);
		return PG_NEXT_NULL;
	}
	if(sys.trg & PAD_BUTTON_A){	//決定ボタン
		pos = wk->sw_st;
		wk->sw_st = (wk->sw_st+1)%SW_MAX;

		PGRadioSub_SwitchStChange(wk,wk->sw_st,pos);
		radiokey_SwitchOn(wk,wk->sw_st);

		Snd_SePlay(SE_PGEAR_RADIO_BUTTON);
		IWASAWA_PRINT(" new pos = %d, old pos = %d\n",wk->sw_st,pos);
////		CATS_ObjectPosSet(wk->pAct[RACT_SEL],wk->x,wk->y);
		return PG_NEXT_NULL;
	}

	//通常キー操作
	if(sys.cont & PAD_KEY_LEFT){
		dx = -KEYMV_DX;
	}
	if(sys.cont & PAD_KEY_RIGHT){
		dx = KEYMV_DX;
	}
	if(sys.cont & PAD_KEY_UP){
		dy = -KEYMV_DY;
	}
	if(sys.cont & PAD_KEY_DOWN){
		dy = KEYMV_DY;
	}
	if(dx == 0 && dy == 0){
		return PG_NEXT_NULL;
	}
	ox = wk->x;
	oy = wk->y;
	if(GF_TP_SingleHitSelf(&(SelectArea_TpCircle),wk->x+dx, wk->y)){
		wk->x += dx;
		if(GF_TP_SingleHitSelf(&(SelectArea_TpCircle),wk->x+dx, wk->y+dy)){
			wk->y += dy;
		}
	}else if(GF_TP_SingleHitSelf(&(SelectArea_TpCircle),wk->x, wk->y+dy)){
		wk->y += dy;
	}
	if(wk->x != ox || wk->y != oy){
		CATS_ObjectPosSet(wk->pAct[RACT_SEL],wk->x,wk->y);
	}
	PGRadioSub_SwitchStChange(wk,SW_MAX,wk->sw_st);
	radiokey_CurrentChannelSet(wk,wk->x,wk->y);
	return PG_NEXT_NULL;
}

/**
 *	@brief	通常アプリタッチ操作
 */
int PGRadioKey_ContTouch(PGRADIO_SYS_WORK* wk,BOOL* touch_f)
{
	int ret;

	*touch_f = FALSE;
	if(wk->touch_st){
		*touch_f = TRUE;
		return radiokey_ContTouchDrug(wk);
	}else{
		ret = radiokey_ContTouchNormal(wk,touch_f);
		if(*touch_f){
			wk->sys->key_mode = APP_KTST_TOUCH;

			if(wk->sys->control == PGCONT_SYS){
				PGRadioSub_ToTouchChange((void*)wk);
			}
		}
		return ret;
	}
	return PG_NEXT_NULL;
}

/**
 * @brief	ポイントの選局チャンネルを取得
 */
u8 PGRadioKey_GetChannelFromPos(PGRADIO_SYS_WORK* wk,s16 x,s16 y,u8* level)
{
	u32 ret;
	u8 new_id;

	ret = GF_TP_HitSelf(ChAreaTbl[wk->area_id],x,y);

//	IWASAWA_PRINT(" pos %d,%d, ret= %d\n",x,y,ret);
	if(	ret == RECT_HIT_NONE){
		new_id = RADIO_CHANNEL_NONE;
		if(level != NULL){
			*level = REC_LEVEL0;
		}
		IWASAWA_PRINT("RetNone\n");
	}else{
		//座標のチャンネルID
		switch(wk->area_id){
		case RADIO_AREA_JOHTO:
		case RADIO_AREA_KANTO:
		case RADIO_AREA_KANTO_EX:
		case RADIO_AREA_KANTO_NONE:
			new_id = RADIO_CH_MUSIC+(ret/2);
			break;
		case RADIO_AREA_ROCKET:
			new_id = RADIO_CH_ROCKET;
			break;
		case RADIO_AREA_AJITO:
			new_id = RADIO_CH_AJITO;
			break;
		case RADIO_AREA_ALF:
		default:
			new_id = RADIO_CH_ALF;
			break;
		}
		if(level != NULL){
			*level = (1-(ret%2))+REC_LEVEL1;
			IWASAWA_PRINT("ChannelHit = %d level = %d\n",new_id,*level);
		}
	}
	return new_id;
}

/**
 *	@brief
 */
static int radiokey_ContTouchNormal(PGRADIO_SYS_WORK* wk,BOOL* touch_f)
{
	int ret;
	RECT_HIT_TBL iconArea;

	static const RECT_HIT_TBL Switch_TpRect[] = {
//		{0,191,0,255}, ty,by,lx,rx
		{SW01_DPY,SW01_DPY+SW_DSY,SW01_DPX,SW01_DPX+SW_DSX},
		{SW02_DPY,SW02_DPY+SW_DSY,SW02_DPX,SW02_DPX+SW_DSX},
		{SW03_DPY,SW03_DPY+SW_DSY,SW03_DPX,SW03_DPX+SW_DSX},
		{SW04_DPY,SW04_DPY+SW_DSY,SW04_DPX,SW04_DPX+SW_DSX},
		{RECT_HIT_END,0,0,0}
	};

	if(!GF_TP_GetTrg()){
		return PG_NEXT_NULL;
	}

	ret = PGFrame_TpControl(wk->sys);
	if(ret != PG_NEXT_NULL){
		*touch_f = TRUE;
		return ret;
	}

	ret = GF_TP_RectHitTrg(Switch_TpRect);
	if(	ret != RECT_HIT_NONE){
		PGRadioSub_SwitchStChange(wk,ret,wk->sw_st);
		radiokey_SwitchOn(wk,ret);

		Snd_SePlay(SE_PGEAR_RADIO_BUTTON);
		wk->sw_st = ret;
		*touch_f = TRUE;

		IWASAWA_PRINT(" new pos = %d, old pos = %d\n",ret,wk->sw_st);
		return PG_NEXT_NULL;
	}

	//選局アイコンを触ったか？
	if(!GF_TP_SingleHitSelf(&(SelectArea_TpCircle),sys.tp_x, sys.tp_y)){
		return PG_NEXT_NULL;
	}
	iconArea.circle.code = TP_USE_CIRCLE;
	iconArea.circle.r = SELICON_R;
	iconArea.circle.x = wk->x;
	iconArea.circle.y = wk->y;
	if(GF_TP_SingleHitSelf(&(iconArea),sys.tp_x,sys.tp_y)){
		wk->x = sys.tp_x;
		wk->y = sys.tp_y;
		CATS_ObjectPosSet(wk->pAct[RACT_SEL],wk->x,wk->y);
		wk->touch_st = 1;
		*touch_f = TRUE;

		PGRadioSub_SwitchStChange(wk,SW_MAX,wk->sw_st);

#if	GS_BTS5616_20090728_FIX
		/*
			選局アイコンの当たり判定内の移動で、選局エリアが切り替わる座標にいるときに
			ここでチャンネルチェックしないと、次のフレームまでにタッチ判定がOffになった場合に
			アイコンだけ移動して、曲が鳴らない(止まらない)ことがあるバグに対処
		*/
		radiokey_CurrentChannelSet(wk,wk->x,wk->y);
#endif
	}
	return PG_NEXT_NULL;
}

/**
 *	@brief	選局アイコンドラッグ操作
 */
static int radiokey_ContTouchDrug(PGRADIO_SYS_WORK* wk)
{
	if(!GF_TP_GetCont()){
		wk->touch_st = 0;
		return PG_NEXT_NULL;
	}
	if(GF_TP_SingleHitSelf(&(SelectArea_TpCircle),sys.tp_x, sys.tp_y)){
		wk->x = sys.tp_x;
		wk->y = sys.tp_y;
		CATS_ObjectPosSet(wk->pAct[RACT_SEL],wk->x,wk->y);
		radiokey_CurrentChannelSet(wk,wk->x,wk->y);
	}
	return PG_NEXT_NULL;
}


/**
 *	@brief	選択チャンネル取得
 */
static u8 radiokey_CurrentChannelSet(PGRADIO_SYS_WORK* wk,s16 x,s16 y)
{
	u8 new_id,new_level = 0;

	new_id = PGRadioKey_GetChannelFromPos(wk,x,y,&new_level);

	if(new_id == RADIO_CHANNEL_NONE){
		if(wk->level_st != REC_LEVEL0){	//プログラムを止める
			PGRadioSub_ProgramEnd(wk);
			Snd_BgmStop(Snd_NowBgmNoGet(),0);
		}
		wk->level_st = REC_LEVEL0;
		wk->ch_id = RADIO_CHANNEL_NONE;
		return 0;
	}
	//プログラム更新チェック
	if(new_id == wk->ch_id){	//idが同じなら受信状態チェックのみ
		//受信状態チェック
		if(new_level != wk->level_st){	//受信レベルが変わった
			if(new_level == REC_LEVEL2){
				PMRadio_ReceptLevelChange(wk->pRadioSys,PMRADIO_REC_NORMAL);
			}else{
				PMRadio_ReceptLevelChange(wk->pRadioSys,PMRADIO_REC_FEEBLE);
			}
			wk->level_st = new_level;
		}
		return FALSE;
	}
	//別番組の再生中なら止める
	if(wk->ch_id != RADIO_CHANNEL_NONE){
		PGRadioSub_ProgramEnd(wk);
	}
	//番組更新
	wk->ch_id = new_id;
	wk->level_st = new_level;
	PGRadioSub_ProgramCall(wk);
	return TRUE;
}

/**
 *	@brief	選局スイッチ動作
 */
static void radiokey_SwitchOn(PGRADIO_SYS_WORK* wk,u8 pos)
{
	wk->x = ChJohto_Circle[pos*2].circle.x;
	wk->y = ChJohto_Circle[pos*2].circle.y;

	CATS_ObjectPosSet(wk->pAct[RACT_SEL],wk->x,wk->y);

	radiokey_CurrentChannelSet(wk,wk->x,wk->y);
//	wk->level_st = REC_LEVEL2;
//	PMRadio_ReceptLevelChange(wk->pRadioSys,PMRADIO_REC_NORMAL);
}

